import request from '@/utils/request'
import { getStorage } from '@/utils/storage'

// 1.获取所有频道
export const getAllChannelsAPI = () => {
  return request({
    url: '/v1_0/channels',
    method: 'get'
  })
}

// 2.用户登录
export const loginAPI = (data) => {
  return request({
    url: '/v1_0/authorizations',
    method: 'post',
    data
  })
}

// 3.获取用户选择的频道
export const getUserChannelsAPI = () => {
  return request({
    url: '/v1_0/user/channels',
    method: 'get'
  })
}

// 4.获取所有推荐文章
export const getAllArticleListAPI = ({ channel_id, timestamp }) => {
  return request({
    url: '/v1_0/articles',
    method: 'get',
    params: {
      channel_id,
      timestamp
    }
  })
}

// 5.对文章不感兴趣
export const artDislikeAPI = ({ artId }) => {
  return request({
    url: '/v1_0/article/dislikes',
    method: 'post',
    data: {
      target: artId
    }
  })
}

// 6.举报文章
export const artRepostAPI = ({ artId, type }) => {
  return request({
    url: '/v1_0/article/reports',
    method: 'post',
    data: {
      target: artId,
      type,
      remark: '暂未实现'
    }
  })
}

// 7.更新覆盖频道
export const updateChannelAPI = ({ channels }) => {
  return request({
    url: '/v1_0/user/channels',
    method: 'put',
    data: {
      channels
    }
  })
}

// 8.搜索联想列表
export const getSuggestListAPI = ({ q }) => {
  return request({
    url: '/v1_0/suggestion',
    method: 'get',
    params: {
      q
    }
  })
}

// 9.搜索结果列表
export const getSearchResultAPI = ({ q, page, per_page }) => {
  return request({
    url: '/v1_0/search',
    method: 'get',
    params: {
      q,
      page,
      per_page
    }
  })
}

// 10.通过id获取文章详情
export const getArticleDetailAPI = (artId) => {
  return request({
    url: `/v1_0/articles/${artId}`,
    method: 'get'
  })
}

// 11.关注用户
export const followUserAPI = ({ autId }) => {
  return request({
    url: '/v1_0/user/followings',
    method: 'post',
    data: {
      target: autId
    }
  })
}

// 12.取消关注用户
export const unFollowUserAPI = ({ autId }) => {
  return request({
    url: `/v1_0/user/followings/${autId}`,
    method: 'delete'
  })
}

// 13.文章点赞
export const loveArticleAPI = ({ artId }) => {
  return request({
    url: '/v1_0/article/likings',
    method: 'post',
    data: {
      target: artId
    }
  })
}

// 14.取消点赞
export const noLoveArticleAPI = ({ artId }) => {
  return request({
    url: `/v1_0/article/likings/${artId}`,
    method: 'delete'
  })
}

// 15.获取文章评论列表
export const getCommentListAPI = ({ artId, offset = null, limit = 10 }) => {
  return request({
    url: '/v1_0/comments',
    method: 'get',
    params: {
      type: 'a',
      source: artId,
      offset,
      limit
    }
  })
}

// 16.评论确定喜爱
export const loveCommentAPI = ({ comId }) => {
  return request({
    url: '/v1_0/comment/likings',
    method: 'post',
    data: {
      target: comId
    }
  })
}

// 17.取消评论喜爱
export const noLoveCommentAPI = ({ comId }) => {
  return request({
    url: `/v1_0/comment/likings/${comId}`,
    method: 'delete'
  })
}

// 18.发表评论- 只有params遇到null才不携带,data依旧会携带
export const publishCommitAPI = ({ id, content, artId = null }) => {
  return request({
    url: '/v1_0/comments',
    method: 'post',
    data: {
      target: id,
      content,
      art_id: artId
    }
  })
}

// 19.获取用户个人信息
export const getUserProfileAPI = () => {
  return request({
    url: '/v1_0/user/profile',
    method: 'get'
  })
}

// 20.获取用户基本信息
export const getUserInfoAPI = () => {
  return request({
    url: '/v1_0/user',
    method: 'get'
  })
}

// 21.更新用户头像
export const updateUserPhotoAPI = (fd) => {
  return request({
    url: '/v1_0/user/photo',
    method: 'patch',
    data: fd
  })
}

// 22.更新用户基本资料
export const updateUserProfileAPI = (data) => {
  return request({
    url: '/v1_0/user/profile',
    method: 'patch',
    data
  })
}

// 23.刷新用户token
export const refreshTokenAPI = () => {
  return request({
    url: '/v1_0/authorizations',
    method: 'put',
    headers: {
      Authorization: 'Bearer ' + getStorage('refresh_token')
    }
  })
}
